package com.controllers;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dao.UserDao;
import com.models.User;
import com.models.User;



@Controller
class UserController {
   @Autowired
   UserDao userdao;
   
   @RequestMapping("/findAll")
   public ModelAndView findAll(){
	   ModelAndView modelAndView=new ModelAndView("/userList");
	   List<User> lists=userdao.findAll();
	   System.out.println(lists.size());
	   modelAndView.addObject("lists",lists);
	return modelAndView;
	   
   }
   @RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("/register");
		return mav;
   }
   @RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(@RequestParam String username,@RequestParam String password ){
	   User user=new User();

	   user.setUsername(username);
	   user.setPassword(password);
	   userdao.addUser(user);
	   ModelAndView mav=new ModelAndView("redirect:/login3");
		return mav;
		
	}
   @RequestMapping(value="/login",method=RequestMethod.GET)
	public ModelAndView login(){
		ModelAndView mav=new ModelAndView("/login");
		return mav;
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ModelAndView login(
			@RequestParam String username,
			@RequestParam String password
			){
		
		User user = userdao.findByName(username);
		
		if(user!=null&&user.getPassword().equals(password)){
			ModelAndView mav=new ModelAndView("redirect:/depot");
		return mav;
		}else{
			ModelAndView mav=new ModelAndView("/login2");
		    System.out.println("用户名或密码错误");
			return mav;
		}
		}
	
	@RequestMapping(value="/login3",method=RequestMethod.GET)
	public ModelAndView login3(){
		ModelAndView mav=new ModelAndView("/login3");
		return mav;
	}
	
	@RequestMapping(value="/login3",method=RequestMethod.POST)
	public ModelAndView login3(
			@RequestParam String username,
			@RequestParam String password
			){
		
		User user = userdao.findByName(username);
		
		if(user!=null&&user.getPassword().equals(password)){
			ModelAndView mav=new ModelAndView("redirect:/depot");
		return mav;
		}else{
			ModelAndView mav=new ModelAndView("/login2");
		    System.out.println("用户名或密码错误");
			return mav;
		}
		}
   @RequestMapping("/depot")
   public String depot(){
	return "/depot";
	   
   }
}
